// pages/doctor-user-detail/doctor-user-detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl, // 线上图片
    localImg: app.globalData.staticimgUrl, // 本地图片
    cur: "1",
    pageNum: 1,
    pageSize: 10,
    customerid: "",
    userInfor: {},
    diaryinfo:[],
    orderList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      customerid: options.customerid
    })
    this.getUserInfor();
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    this.getDiaryData();
  },
  // 切换
  tabCurrent(e) {
    const cur = e.target.dataset.index;
    3 == cur && this.getOrderDetail();
    this.setData({
      cur
    })
  },
  // 用户资料
  getUserInfor() {
    const that = this;
    const param = {
      userId: this.data.customerid
    }
    app.request({
      url: "doctor/member/userDetail",
      data: param,
      success: function(res) {
        that.setData({
          userInfor: res.data
        })
      },
      fail: function(res) {
        // app.requestFail(res);
      }
    });
  },
  // 关注
  follow(e) {
    const userid = e.currentTarget.dataset.userid;
    const that = this;
    app.request({
      url: 'doctor/member/attention',
      data: {
        userId: userid,
      },
      success(res) {
        app.showToast(res.message)
        that.getUserInfor();
      }
    })

  },
  // 日记
  getDiaryData() {
    const that = this;
    const param = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      userId: this.data.customerid
    };
    app.request({
      url: "doctor/member/getPage",
      data: param,
      success: function(res) {
        that.setData({
          diaryinfo: res.data
        })
      },
      fail: function(res) {
        // app.requestFail(res);
      }
    });

  },
  // 用户日记页
  entry(e){
    const bookid = e.currentTarget.dataset.bookid;
    const diaryId = e.currentTarget.dataset.diaryid;
    wx.navigateTo({
      url: "../user-diary/user-diary?bookid=" + bookid + "&diaryId=" + diaryId
    });
  },
  // 点赞
  thumbsUp(e){
    const that = this;
    const diaryId = e.currentTarget.dataset.diaryid;
    app.request({
      url: "doctor/diary/praise",
      data: {
        diaryId
      },
      success: function (res) {
        that.getDiaryData();
      },
      fail: function (res) { }
    });

  },
  // 评论
  entryComment(e) {
    const bookid = e.currentTarget.dataset.bookid;
    const diaryId = e.currentTarget.dataset.diaryid;
    wx.navigateTo({
      url: "../user-diary-details/user-diary-details?diaryId=" + diaryId + "&bookid=" + bookid
    });
  },
  // 预览大图
  previewImage(e) {
    const urls = [];
    const imgArr = this.data.orderList;
    const current = e.currentTarget.dataset.src;
    console.log(imgArr)
    for (let i = 0; i < imgArr.length; i++) {
      for (let j = 0; j < imgArr[i].comment.length; j++) {
        for (let k = 0; k < imgArr[i].comment[j].commentEnclosureArray.length; k++) {
          urls.push(this.data.imgBase + imgArr[i].comment[j].commentEnclosureArray[k].imgPath)
        }
      }
    }
    wx.previewImage({
      urls,
      current
    })
  },
  // 订单信息
  getOrderDetail() {
    const that = this;
    app.request({
      url: "shopp/orderApi/getMyCustomerByOrderInfo",
      data: {
        loginUserId: wx.getStorageSync('userid'),
        customerId: that.data.customerid
      },
      success: function(res) {
        that.setData({
          orderList: res.data
        })
      },
      fail: function(res) {}
    });
  },
  // 订单详情
  enrtyDetail(e){
    console.log(e.currentTarget.dataset.orderid)
    const orderid = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '/pages/doctor-customer-order-detail/doctor-customer-order-detail?orderid=' + orderid
    });
  },
  // 加载更多
  moreInfo() {
    let page = this.data.page + 1;
    this.setData({
      page
    });
    this.getOrderDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})